// PageDICOM.cpp : implementation file
//

#include "stdafx.h"
#include "ECGWorkStation.h"
#include "PageDICOM.h"
#include "globals.h"
#include "..\public\pubfun\inireadwrite\inireadwrite.h"
#include "EchoSCU.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPageDICOM property page

IMPLEMENT_DYNCREATE(CPageDICOM, CPropertyPage)

CPageDICOM::CPageDICOM() : CPropertyPage(CPageDICOM::IDD)
{
	//{{AFX_DATA_INIT(CPageDICOM)
	m_strLocalAE = _T("");
	m_strLocalPort = _T("");
	m_strRemoteAE = _T("");
	m_strRemotePort = _T("");
	m_bAutoSend = FALSE;
	m_strRemoteIP = _T("");
	//}}AFX_DATA_INIT
}

CPageDICOM::~CPageDICOM()
{
}

void CPageDICOM::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageDICOM)
	DDX_Text(pDX, IDC_EDIT_LOCAL_AE, m_strLocalAE);
	DDX_Text(pDX, IDC_EDIT_LOCAL_PORT, m_strLocalPort);
	DDX_Text(pDX, IDC_EDIT_REMOTE_AE, m_strRemoteAE);
	DDX_Text(pDX, IDC_EDIT_REMOTE_PORT, m_strRemotePort);
	DDX_Check(pDX, IDC_CHECK_AUTOSEND, m_bAutoSend);
	DDX_Text(pDX, IDC_EDIT_REMOTE_IP, m_strRemoteIP);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageDICOM, CPropertyPage)
	//{{AFX_MSG_MAP(CPageDICOM)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_ECHO, OnButtonEcho)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageDICOM message handlers

BOOL CPageDICOM::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	CIniReadWrite::GetMe()->LoadString(DEF_DICOM_SETTING, DEF_DICOM_LOCAL_AE, &m_strLocalAE);
	CIniReadWrite::GetMe()->LoadString(DEF_DICOM_SETTING, DEF_DICOM_LOCAL_PORT, &m_strLocalPort);

	CIniReadWrite::GetMe()->LoadString(DEF_DICOM_SETTING, DEF_DICOM_REMOTE_AE, &m_strRemoteAE);
	CIniReadWrite::GetMe()->LoadString(DEF_DICOM_SETTING, DEF_DICOM_REMOTE_PORT, &m_strRemotePort);
	CIniReadWrite::GetMe()->LoadString(DEF_DICOM_SETTING, DEF_DICOM_REMOTE_IP, &m_strRemoteIP);

	CIniReadWrite::GetMe()->LoadInt(DEF_DICOM_SETTING, DEF_DICOM_AUTO_SEND, &m_bAutoSend);
	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPageDICOM::OnDestroy() 
{
	CPropertyPage::OnDestroy();
	
	UpdateData(TRUE);
}

void CPageDICOM::OnButtonEcho() 
{
	BeginWaitCursor();
	CEchoSCU::Create();
	UpdateData(TRUE);

	int nRet = 0;
	if(nRet = CEchoSCU::GetMe()->Echo((char*)(LPCTSTR)m_strLocalAE, (char*)(LPCTSTR)m_strRemoteAE, (char*)(LPCTSTR)m_strRemoteIP, atoi((LPCTSTR)m_strRemotePort))){
		AfxMessageBox("Echo failed.");
	}else{
		AfxMessageBox("Echo Succeed.");
	}
	CEchoSCU::Destroy();
	EndWaitCursor();
}

void CPageDICOM::SaveSetting()
{
	CIniReadWrite::GetMe()->SaveString(DEF_DICOM_SETTING, DEF_DICOM_LOCAL_AE, m_strLocalAE);
	CIniReadWrite::GetMe()->SaveString(DEF_DICOM_SETTING, DEF_DICOM_LOCAL_PORT, m_strLocalPort);

	CIniReadWrite::GetMe()->SaveString(DEF_DICOM_SETTING, DEF_DICOM_REMOTE_AE, m_strRemoteAE);
	CIniReadWrite::GetMe()->SaveString(DEF_DICOM_SETTING, DEF_DICOM_REMOTE_PORT, m_strRemotePort);
	CIniReadWrite::GetMe()->SaveString(DEF_DICOM_SETTING, DEF_DICOM_REMOTE_IP, m_strRemoteIP);

	CIniReadWrite::GetMe()->SaveInt(DEF_DICOM_SETTING, DEF_DICOM_AUTO_SEND, m_bAutoSend);
}
